package com.zhiyun.service.impl;

import com.zhiyun.domain.Course;
import com.zhiyun.domain.Student;
import com.zhiyun.domain.Teacher;
import com.zhiyun.domain.User;
import com.zhiyun.mapper.*;
import com.zhiyun.service.UserService;
import com.zhiyun.util.Db;
import com.zhiyun.vo.Result;

import java.util.List;

/**
 * @author zc
 * @Date 2025/4/29 上午8:18
 */
public class UserServiceImpl implements UserService {
    UserMapper userMapper = Db.getMapper(UserMapper.class);
    StudentMapper studentMapper = Db.getMapper(StudentMapper.class);
    TeacherMapper teacherMapper = Db.getMapper(TeacherMapper.class);

    @Override
    public Result studentLogin(User user) {
        User oldUser = userMapper.selectByUserNameAndRoleID(user.getUsername(), user.getRoleId());
        if (oldUser == null) {
            return Result.fail("用户不存在");
        } else {
            if (!oldUser.getPassword().equals(user.getPassword())) {
                return Result.fail("密码错误");
            } else {
                Student student = studentMapper.selectByUserId(oldUser.getId());
                return Result.ok("登录成功", student);
            }
        }
    }

    @Override
    public Result teacherLogin(User user) {
        User oldUser = userMapper.selectByUserNameAndRoleID(user.getUsername(), user.getRoleId());
        if (oldUser == null) {
            return Result.fail("用户不存在");
        } else {
            if (!oldUser.getPassword().equals(user.getPassword())) {
                return Result.fail("密码错误");
            } else {
                Teacher teacher = teacherMapper.selectByUserId(oldUser.getId());
                return Result.ok("登录成功", teacher);
            }
        }
    }

    @Override
    public Result studentRegister(Student student) {
        try {
            Db.startTransaction();
            User oldUser = userMapper.selectByUserNameAndRoleID(student.getUser().getUsername(), student.getUser().getRoleId());
            if (oldUser != null) {
                return Result.fail("注册失败,该用户已存在");
            }
            int rows = userMapper.insertWithAutoKey(student.getUser());
            student.setUserId(student.getUser().getId());
            int rows1 = studentMapper.insert(student);
            Db.commitTransaction();
            if (rows + rows1 == 2) {
                return Result.okMsg("注册成功");
            }
        } catch (Exception e) {
            Db.rollbackTransaction();
        } finally {
            Db.close();
        }
        return Result.fail("注册失败");
    }

    @Override
    public Result teacherRegister(Teacher teacher) {
        try {
            Db.startTransaction();
            User oldUser = userMapper.selectByUserNameAndRoleID(teacher.getUser().getUsername(), teacher.getUser().getRoleId());
            if (oldUser != null) {
                return Result.fail("注册失败,该用户已存在");
            }
            int rows = userMapper.insertWithAutoKey(teacher.getUser());
            teacher.setUserId(teacher.getUser().getId());
            int rows1 = teacherMapper.insert(teacher);
            Db.commitTransaction();
            if (rows + rows1 == 2) {
                return Result.okMsg("注册成功");
            }
        } catch (Exception e) {
            e.printStackTrace();
            Db.rollbackTransaction();
        } finally {
            Db.close();
        }
        return Result.fail("注册失败");
    }

    @Override
    public Result UpdatePassword(Integer userId,String password) {
        User oldUser = userMapper.selectByPrimaryKey(userId);
        if (oldUser == null) {
            return Result.fail("用户不存在");
        }
        if (oldUser.getPassword().equals(password)){
            return Result.fail("旧密码不能和原密码相同");
        }
        oldUser.setPassword(password);
        int rows = userMapper.updateByPrimaryKey(oldUser);
        if (rows > 0) {
            return Result.okMsg("修改成功");
        } else {
            return Result.fail("修改失败");
        }
    }

}
